package com.rocksdata.mc.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.rocksdata.mc.entity.CampusAppInfo;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈堂正 on 2017/4/5.
 */

public class ApplicationInfoDBManager {
    private static ApplicationInfoDBManager instance = null;

    private ApplicationInfoDBManager(){

    }

    static synchronized ApplicationInfoDBManager getInstance(){
        if(instance == null){
            instance = new ApplicationInfoDBManager();
        }
        return instance;
    }

    public List<CampusAppInfo> getAllAppList(){
        Cursor c = DatabaseManager.getDatabase().query(DatabaseHelper.TApplicationInfo.TABLE_NAME, null,
                null, null, null, null, null);
        List<CampusAppInfo> appList = new ArrayList<>();
        while(c.moveToNext()){
            CampusAppInfo info = new CampusAppInfo();
            info.setCode(c.getString(c.getColumnIndex(DatabaseHelper.TApplicationInfo.APP_CODE)));
            info.setName(c.getString(c.getColumnIndex(DatabaseHelper.TApplicationInfo.APP_NAME)));
            info.setIcon(c.getString(c.getColumnIndex(DatabaseHelper.TApplicationInfo.APP_ICON)));
            info.setGroup(c.getString(c.getColumnIndex(DatabaseHelper.TApplicationInfo.APP_GROUP)));
            info.setIs_usable(c.getInt(c.getColumnIndex(DatabaseHelper.TApplicationInfo.APP_USABLE)));
            appList.add(info);
        }
        c.close();

        return appList;
    }

    public List<CampusAppInfo> getAppListByCodes(List<String> codes){
        List<CampusAppInfo> result = new ArrayList<>();
        for (String code: codes) {
            Cursor c = DatabaseManager.getDatabase().query(DatabaseHelper.TApplicationInfo.TABLE_NAME, null,
                    DatabaseHelper.TApplicationInfo.APP_CODE + "=?", new String[]{code}, null, null, null);
            if (c.moveToFirst()) {
                CampusAppInfo info = new CampusAppInfo();
                info.setCode(c.getString(c.getColumnIndex(DatabaseHelper.TApplicationInfo.APP_CODE)));
                info.setName(c.getString(c.getColumnIndex(DatabaseHelper.TApplicationInfo.APP_NAME)));
                info.setIcon(c.getString(c.getColumnIndex(DatabaseHelper.TApplicationInfo.APP_ICON)));
                info.setGroup(c.getString(c.getColumnIndex(DatabaseHelper.TApplicationInfo.APP_GROUP)));
                info.setIs_usable(c.getInt(c.getColumnIndex(DatabaseHelper.TApplicationInfo.APP_USABLE)));
                result.add(info);
            }
            c.close();
        }
        return result;
    }

    public void deleteAllApp(){
        DatabaseManager.getDatabase().delete(DatabaseHelper.TApplicationInfo.TABLE_NAME, null, null);
    }

    public void saveAppList(List<CampusAppInfo> list){
        if(list == null || list.size() == 0) return;
        SQLiteDatabase db  = DatabaseManager.getDatabase();
        db.beginTransaction();
        try {
            for (CampusAppInfo info: list) {
                ContentValues values = new ContentValues();
                values.put(DatabaseHelper.TApplicationInfo.APP_CODE, info.getCode());
                values.put(DatabaseHelper.TApplicationInfo.APP_NAME, info.getName());
                values.put(DatabaseHelper.TApplicationInfo.APP_ICON, info.getIcon());
                values.put(DatabaseHelper.TApplicationInfo.APP_GROUP, info.getGroup());
                values.put(DatabaseHelper.TApplicationInfo.APP_USABLE, info.getIs_usable());

                db.insert(DatabaseHelper.TApplicationInfo.TABLE_NAME, null, values);
            }
            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
        }
    }

}
